package com.da.lon.wang.xlg.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ant.liao.GifView;
import com.ant.liao.GifView.GifImageType;
import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.SendAddressAdapter;
import com.da.lon.wang.xlg.bean.DetailBean;
import com.da.lon.wang.xlg.bean.DetailBean.FhName;
import com.da.lon.wang.xlg.bean.DetailBean.NomeList.Value;
import com.da.lon.wang.xlg.bean.GoodResult;
import com.da.lon.wang.xlg.bean.GoodSattr;
import com.da.lon.wang.xlg.bean.IdsBean;
import com.da.lon.wang.xlg.bean.OrderListBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.util.BuyUtil;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.SingleManager;
import com.da.lon.wang.xlg.util.ToastUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;
import java.util.List;

public class ProductsPopupWindow extends PopupWindow implements
        OnClickListener, android.widget.CompoundButton.OnCheckedChangeListener {

    public final static int SHOWPPW = 0;
    CallBack3 myCallBack;
    private View mMenuView;
    private String product_number;
    private String product_id;
    private HttpLoader<GoodSattr> uploader;
    private DetailBean bean = new DetailBean();
    private HttpLoader<IdsBean> loader6;
    private HttpLoader<OrderListBean> loader3;
    private String countnumber;
    private ImageView photoView;
    private TextView nameView, stockView, priceView, countView;
    private LinearLayout popLayout;
    private FlowRadioGroup nomeGroup;
    private RadioButton nomeButton;
    private ImageView closeView;
    private RadioButton[][] mrRadioButtons;
    private RadioGroup[] mGroups;
    private ImageButton addButton, minusButton;
    private int[] selected;
    private Button okButton;
    private Context mContext;
    private Dialog dialog;
    private View dialogView;
    private GifView dialogGifView;
    private String realStock;
    private boolean flg = true;
    private int time = 1;
    private int isBuy = 0;
    private View lineView;

    // 选择发货地址
    private ScrollGridView send_address_gv;

    private Button ouka_products_addcart_bt;
    private String send_address_id = "";
    private LinearLayout peisong_address_ll;
    private View popupwindowLayout;
    private BuyUtil util;
    private ScrollView fh_address_sv;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case SHOWPPW:
                    int fh_address_sv_height = fh_address_sv.getHeight();
                    if (fh_address_sv_height > 600) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LayoutParams.MATCH_PARENT, 600);
                        fh_address_sv.setLayoutParams(params);
                    }
                    break;

                default:
                    break;
            }
        }

        ;
    };
    private LinearLayout skill_buy_ll;

    public ProductsPopupWindow(Context context, DetailBean bean2, int isBuy,
                               CallBack3 myCallBack, View popupwindowLayout) {
        super(context);
        this.mContext = context;
        this.bean = bean2;
        this.isBuy = isBuy;
        this.myCallBack = myCallBack;
        this.popupwindowLayout = popupwindowLayout;
        product_number = bean2.getProduct_number();
        product_id = bean2.getId();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(R.layout.ouka_products_popupwindow, null);

        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        util = new BuyUtil(mContext);
        fh_address_sv = (ScrollView) mMenuView.findViewById(R.id.fh_address_sv);
        photoView = (ImageView) mMenuView.findViewById(R.id.detail_pop_photo);
        nameView = (TextView) mMenuView.findViewById(R.id.detail_pop_name);
        stockView = (TextView) mMenuView.findViewById(R.id.detail_pop_stock);
        priceView = (TextView) mMenuView.findViewById(R.id.detail_pop_price);
        popLayout = (LinearLayout) mMenuView
                .findViewById(R.id.detail_pop_layout);
        // fh_address_ll = (LinearLayout) mMenuView
        // .findViewById(R.id.fh_address_ll);
        peisong_address_ll = (LinearLayout) mMenuView
                .findViewById(R.id.peisong_address_ll);
        skill_buy_ll = (LinearLayout) mMenuView.findViewById(R.id.skill_buy_ll);
        countView = (TextView) mMenuView.findViewById(R.id.detail_pop_count);
        okButton = (Button) mMenuView.findViewById(R.id.ouka_products_bottom);
        ouka_products_addcart_bt = (Button) mMenuView
                .findViewById(R.id.ouka_products_addcart_bt);
        addButton = (ImageButton) mMenuView.findViewById(R.id.detail_pop_add);
        minusButton = (ImageButton) mMenuView
                .findViewById(R.id.detail_pop_minus);
        send_address_gv = (ScrollGridView) mMenuView
                .findViewById(R.id.send_address_gv);
        lineView = (View) mMenuView.findViewById(R.id.detail_pop_line);

        if (!"".equals(bean.getIsdown())) {
            if (null != bean.getIsdown()
                    && Integer.parseInt(bean.getIsdown()) == 0) {
                okButton.setBackgroundColor(Color.parseColor("#bfbfbf"));
            }
        }

        if (null != bean.getIs_display() && bean.getIs_display().equals("0")
                && bean.getMs() != null && bean.getMs().getE_time() != 0
                && bean.getMs().getS_time() == 0) {
            // 咖啡参与秒杀
            peisong_address_ll.setVisibility(View.GONE);
            ouka_products_addcart_bt.setVisibility(View.GONE);
            lineView.setVisibility(View.GONE);
            skill_buy_ll.setVisibility(View.GONE);
            okButton.setText("立即秒杀");
        } else if (null != bean.getIs_display()
                && bean.getIs_display().equals("0")) {
            // 咖啡不参与秒杀
            peisong_address_ll.setVisibility(View.GONE);
            ouka_products_addcart_bt.setVisibility(View.GONE);
            skill_buy_ll.setVisibility(View.VISIBLE);
            lineView.setVisibility(View.GONE);
        } else if (bean.getMs() != null && bean.getMs().getE_time() != 0
                && bean.getMs().getS_time() == 0) {
            // 普通商品参与秒杀
            peisong_address_ll.setVisibility(View.VISIBLE);
            ouka_products_addcart_bt.setVisibility(View.GONE);
            lineView.setVisibility(View.VISIBLE);
            skill_buy_ll.setVisibility(View.GONE);
            okButton.setText("立即秒杀");
        } else {
            peisong_address_ll.setVisibility(View.VISIBLE);
            ouka_products_addcart_bt.setVisibility(View.VISIBLE);
            lineView.setVisibility(View.VISIBLE);
            skill_buy_ll.setVisibility(View.VISIBLE);
        }

        addButton.setOnClickListener(this);
        minusButton.setOnClickListener(this);
        okButton.setOnClickListener(this);
        ouka_products_addcart_bt.setOnClickListener(this);

        closeView = (ImageView) mMenuView.findViewById(R.id.detail_pop_close);
        closeView.setOnClickListener(this);

        PreferenceUtils.getInstance().SetSettingString(
                PreferencesConfig.COUNT_NUMBER, countView.getText().toString());

        dialogView = LayoutInflater.from(mContext).inflate(
                R.layout.ocoffee_loading_dialog, null);
        dialogGifView = (GifView) dialogView.findViewById(R.id.loading_gif);
        dialogGifView.setGifImage(R.drawable.gif);
        dialogGifView.setGifImageType(GifImageType.COVER);

        // 初始化选择提货地址的控件
        initChoiseTypeView(context);
        // 初始化商品属性的控件
        initView(context);

    }

    private void initChoiseTypeView(Context context) {

        MyApplication.getInstance().setFhnamelist(bean.getFhname());

        final SendAddressAdapter adapter = new SendAddressAdapter(context);
        send_address_gv.setAdapter(adapter);
        send_address_gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                adapter.chooceState(position);
                adapter.notifyDataSetChanged();
                getGoodsStore(0);
            }
        });
        // 动态改变窗口宽高
        handler.sendEmptyMessageDelayed(SHOWPPW, 60);
    }

    private void initView(Context context) {
        try {
            SingleManager manager = SingleManager.getInstance();
            int[] mySelect = manager.getMap(bean.getId());
            MyApplication.loadImageByPicasso(context, bean.getBigimage(), null,
                    photoView, 0, 0, 1);
            nameView.setText(bean.getName());
            realStock = bean.getStock();
            if (null != bean && bean.getStock() != null
                    && Integer.parseInt(bean.getStock()) != 0) {
                // stockView.setText("库存:" + good.getStock() + "件");
                stockView.setText("库存:有货");
            } else {
                stockView.setText("库存:有货");
            }

            if (bean.getMs() != null && bean.getMs().getE_time() != 0
                    && bean.getMs().getS_time() == 0) {
                priceView.setText("秒杀价:￥" + bean.getMs().getSeckill_price());
            } else {
                priceView.setText("￥" + bean.getPricespe());
            }

            mrRadioButtons = new RadioButton[bean.getNome_list().size()][];
            selected = new int[bean.getNome_list().size()];
            mGroups = new RadioGroup[bean.getNome_list().size()];
            for (int i = 0; i < bean.getNome_list().size(); i++) {
                mrRadioButtons[i] = new RadioButton[bean.getNome_list().get(i)
                        .getValue().size()];
                LinearLayout listLayout = new LinearLayout(context);
                listLayout.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                listLayout.setOrientation(LinearLayout.VERTICAL);

                TextView nomeTextView = new TextView(context);
                LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                layoutParams1.setMargins(0, 10, 0, 10);
                nomeTextView.setLayoutParams(layoutParams1);
                nomeTextView.setText(bean.getNome_list().get(i).getName());
                nomeTextView.setTextColor(Color.parseColor("#bfbfbf"));
                nomeTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

                nomeGroup = new FlowRadioGroup(context);

                nomeGroup.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                mGroups[i] = nomeGroup;
                for (int j = 0; j < bean.getNome_list().get(i).getValue()
                        .size(); j++) {
                    nomeButton = new RadioButton(context);
                    nomeButton.setChecked(false);
                    nomeButton.setOnCheckedChangeListener(this);
                    nomeButton.setTag(i);
                    nomeButton.setButtonDrawable(android.R.color.transparent);
                    nomeButton
                            .setBackgroundResource(R.drawable.ouka_detail_radio);
                    nomeButton.setId(i * 100 + j);
                    String replaceAll = bean.getNome_list().get(i).getValue()
                            .get(j).getAttr_value().trim();
                    nomeButton.setText(replaceAll);
                    nomeButton.setGravity(Gravity.CENTER);
                    nomeButton.setPadding(20, 5, 20, 5);

                    nomeButton.setTextColor(Color.parseColor("#333333"));
                    nomeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

                    nomeGroup.addView(nomeButton);
                    mrRadioButtons[i][j] = nomeButton;
                }

                listLayout.addView(nomeTextView);
                listLayout.addView(nomeGroup);

                popLayout.addView(listLayout);
            }

            if (mySelect != null) {
                for (int s = 0; s < bean.getNome_list().size(); s++) {
                    for (int k = 0; k < mrRadioButtons[s].length; k++) {
                        if (mySelect[s] == k) {

                            mGroups[s].check(s * 100 + k);
                        }

                    }
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_pop_close:
                dismiss();
                break;
            case R.id.detail_pop_add:
                countnumber = countView.getText().toString();
                int countnumberAdd = Integer.parseInt(countnumber);
                countnumberAdd++;

                PreferenceUtils.getInstance().SetSettingString(
                        PreferencesConfig.COUNT_NUMBER, countnumberAdd + "");
                getSendAddressId();
                if (!isOK() || "".equals(send_address_id)) {
                    countView.setText(countnumberAdd + "");
                }
                getGoodsStore(1);
                break;
            case R.id.detail_pop_minus:
                flg = true;
                countnumber = countView.getText().toString();
                int countnumberMinus = Integer.parseInt(countnumber);
                if (countnumberMinus > 1) {
                    countnumberMinus--;
                    countView.setText(countnumberMinus + "");
                    PreferenceUtils.getInstance().SetSettingString(
                            PreferencesConfig.COUNT_NUMBER, countnumberMinus + "");
                    getGoodsStore(0);
                }
                break;

            // 加入购物车
            case R.id.ouka_products_addcart_bt:
                getSendAddressId();
                if (send_address_id != null && !"".equals(send_address_id)) {
                    if (isOK()) {
                        myCallBack.click(v, send_address_id, this);
                        // dismiss();
                    } else {
                        ToastUtils.showShort(mContext, "还未选择属性！");
                    }
                } else {
                    ToastUtils.showShort(mContext, "请选择配送方式！");
                }

                break;

            // 立即购买
            case R.id.ouka_products_bottom:
                getSendAddressId();
                if (send_address_id != null && !"".equals(send_address_id)) {
                    if (isOK()) {
                        String value = "";
                        String attr = "";
                        String pid = bean.getId();
                        int[] mySelect = SingleManager.getInstance().getMap(pid);
                        if (bean != null && bean.getNome_list() != null
                                && bean.getNome_list().size() == 0) {
                            value = "{}";
                        } else if (mySelect != null) {
                            value = getCartValue();
                            attr = getValue();
                        }

                        if (null != bean.getIs_display()
                                && !"".equals(bean.getIs_display())
                                && Integer.parseInt(bean.getIs_display()) == 0) {
                            // 购买咖啡
                            util.buyClick(pid, value, attr, true, send_address_id,
                                    bean.getKefu_phone(), popupwindowLayout, this,
                                    myCallBack);
                        } else {
                            // 购买普通商品 参与秒杀
                            if (bean.getMs() != null
                                    && bean.getMs().getE_time() != 0
                                    && bean.getMs().getS_time() == 0) {
                                util.buyClickForSeckill(bean.getProduct_number(),
                                        bean.getMs().getStore_id(), bean.getMs().getIs_card(), value, attr,
                                        false, send_address_id, popupwindowLayout,
                                        this, myCallBack);
                            } else {
                                // 购买普通商品 不参与秒杀
                                util.buyClick(pid, value, attr, false,
                                        send_address_id, bean.getKefu_phone(),
                                        popupwindowLayout, this, myCallBack);
                            }

                        }
                    } else {

                        ToastUtils.showShort(mContext, "还未选择属性！");
                    }
                } else {
                    ToastUtils.showShort(mContext, "请选择配送方式！");
                }

                break;
            // case R.id.pro_address:
            // 点击提货地址，跳转到选择提货地址界面
            // break;
            default:
                break;
        }
    }

    public String getValue() {
        String parm1 = "";
        String parm2 = "";
        List<MyValue> myValues = new ArrayList<ProductsPopupWindow.MyValue>();
        for (int i = 0; i < selected.length; i++) {
            MyValue mvalue = new MyValue();
            mvalue.name = bean.getNome_list().get(i).getName();
            int index = selected[i];
            Value va = bean.getNome_list().get(i).getValue().get(index);
            mvalue.value = va.getAttr_value();
            mvalue.values_all = va.getValues_all();
            myValues.add(mvalue);
        }
        parm2 = "{";
        for (int i = 0; i < myValues.size(); i++) {
            MyValue value = myValues.get(i);
            parm1 = parm1 + value.values_all + ",";
            parm2 = parm2 + "\"" + i + "\"" + ":{" + "\"name\":" + "\""
                    + value.name + "\"" + "," + "\"value\":\"" + value.value
                    + "\"},";

        }
        parm1 = parm1.substring(0, parm1.length() - 1);
        parm2 = parm2.substring(0, parm2.length() - 1);
        parm2 = parm2 + "}";
        return parm1;
    }

    public String getCartValue() {
        String parm1 = "";
        String parm2 = "";
        List<MyValue> myValues = new ArrayList<ProductsPopupWindow.MyValue>();
        for (int i = 0; i < selected.length; i++) {
            MyValue mvalue = new MyValue();
            mvalue.name = bean.getNome_list().get(i).getName();
            int index = selected[i];
            Value va = bean.getNome_list().get(i).getValue().get(index);
            mvalue.value = va.getAttr_value();
            mvalue.values_all = va.getValues_all();
            myValues.add(mvalue);
        }
        parm2 = "{";
        for (int i = 0; i < myValues.size(); i++) {
            MyValue value = myValues.get(i);
            parm1 = parm1 + value.values_all + ",";
            parm2 = parm2 + "\"" + i + "\"" + ":{" + "\"name\":" + "\""
                    + value.name + "\"" + "," + "\"value\":\"" + value.value
                    + "\"},";

        }
        parm1 = parm1.substring(0, parm1.length() - 1);
        parm2 = parm2.substring(0, parm2.length() - 1);
        parm2 = parm2 + "}";
        return parm2;
    }

    /**
     * 选择商品属性
     *
     * @return
     */
    public boolean isOK() {
        int sum = 0;
        if (mrRadioButtons != null) {
            for (int i = 0; i < mrRadioButtons.length; i++) {
                boolean ok = false;
                for (int j = 0; j < mrRadioButtons[i].length; j++) {
                    if (mrRadioButtons[i][j].isChecked()) {
                        ok = true;
                        selected[i] = j;
                    }
                }
                if (ok) {
                    sum++;
                }
            }
            if (null != mrRadioButtons && sum == mrRadioButtons.length) {
                SingleManager manager = SingleManager.getInstance();
                manager.setMap(bean.getId(), selected);
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * 选择商品属性
     *
     * @return
     */
    public void getSendAddressId() {

        if (null != bean.getIs_display() && bean.getIs_display().equals("0")) {
            // 咖啡
            send_address_id = bean.getFhname().get(0).getSend_mothed_id();
        } else {// 普通商品
            ArrayList<FhName> fhnamelist = MyApplication.getInstance()
                    .getFhnamelist();
            for (int i = 0; null != fhnamelist && i < fhnamelist.size(); i++) {
                if (fhnamelist.get(i).isSelect()) {
                    send_address_id = MyApplication.getInstance()
                            .getFhnamelist().get(i).getSend_mothed_id();
                }
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        flg = true;
        time++;
        if (time % 2 != 0) {
            return;
        }
        getGoodsStore(0);
    }

    /**
     * 获取商品库存 type = 1 表示点击的+号 type=0 表示点击的其他按钮
     */
    private void getGoodsStore(final int type) {
        if (bean.getMs() != null && bean.getMs().getE_time() != 0
                && bean.getMs().getS_time() == 0) {
            // 秒杀不改变 库存价格
            return;
        }
        getSendAddressId();
        if (isOK() && null != send_address_id && !"".equals(send_address_id)) {
            String va = "";
            String pid = bean.getId();
            int[] mySelect = SingleManager.getInstance().getMap(pid);
            if (bean != null && bean.getNome_list() != null
                    && bean.getNome_list().size() == 0) {
            } else if (mySelect != null) {
                va = getValue();
            }

            // 获取库存数量
            uploader = new HttpLoader<GoodSattr>(mContext, XLGConstants.URL
                    + "/Product-goodsattr", GoodSattr.class, 60 * 1000);
            AjaxParams params = new AjaxParams();

            params.put("product_number", product_number);
            params.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                    + "goodsattr"));
            params.put("attrid", va);
            params.put("fhtype", send_address_id);
            params.put(
                    "count",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.COUNT_NUMBER, "1"));

            uploader.setManagerListener(new OnWebLoadListener<GoodSattr>() {

                @Override
                public void OnStart() {
                    showDialog();
                    for (int i = 0; null != nomeGroup
                            && i < nomeGroup.getChildCount(); i++) {
                        nomeGroup.getChildAt(i).setEnabled(false);
                    }
                }

                @Override
                public void OnError(String error) {
                    dismissDialog();
                    for (int i = 0; null != nomeGroup
                            && i < nomeGroup.getChildCount(); i++) {
                        nomeGroup.getChildAt(i).setEnabled(true);
                    }
                }

                @Override
                public void onSuccess(GoodSattr t) {
                    dismissDialog();
                    if (t != null && t.getCode().equals("000000")) {
                        GoodResult good = t.getResult();
                        realStock = good.getStock();
                        for (int i = 0; null != nomeGroup
                                && i < nomeGroup.getChildCount(); i++) {
                            nomeGroup.getChildAt(i).setEnabled(true);
                        }
                        if (null != good && good.getStock() != null
                                && Integer.parseInt(good.getStock()) != 0) {
                            stockView.setText("库存:有货");
                            priceView.setText("￥" + good.getPrice());
                            countView
                                    .setText(PreferenceUtils
                                            .getInstance()
                                            .getSettingStr(
                                                    PreferencesConfig.COUNT_NUMBER,
                                                    "1"));
                        } else {
                            priceView.setText("￥" + good.getPrice());
                            if (type == 1) {
                                myCallBack.click(ouka_products_addcart_bt,
                                        send_address_id,
                                        ProductsPopupWindow.this);
                                countnumber = PreferenceUtils.getInstance()
                                        .getSettingStr(
                                                PreferencesConfig.COUNT_NUMBER,
                                                "1");
                                int countnumberMinus = Integer
                                        .parseInt(countnumber);
                                if (countnumberMinus > 1) {
                                    countnumberMinus--;
                                    PreferenceUtils
                                            .getInstance()
                                            .SetSettingString(
                                                    PreferencesConfig.COUNT_NUMBER,
                                                    countnumberMinus + "");
                                }
                            } else {
                                stockView.setText("库存:无货");
                            }
                        }
                    } else {
                        GoodResult good = t.getResult();
                        if (null != good) {
                            realStock = good.getStock();
                            stockView.setText("库存:无货");
                            priceView.setText("￥" + good.getPrice());
                        }
                    }

                }
            });
            uploader.loadData(params);
        } else {
            // ToastUtils.showShort(mContext, "请选择属性！");
        }
    }

    private void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    private void showDialog() {
        this.showDialog(null);
    }

    private void showDialog(CharSequence title) {
        if (dialog == null) {
            dialog = new Dialog(mContext, R.style.ocoffee_loading_dialog);
            dialog.setCanceledOnTouchOutside(true);
            dialog.setContentView(dialogView);
        }
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public BuyUtil getUtil() {
        return util;
    }

    /*
     * 回调按钮点击事件到Activity
     */
    public interface CallBack3 {
        public void click(View v, String send_address_id,
                          PopupWindow product_popupWindow);
    }

    private class MyValue {
        public String name;
        public String value;
        private String values_all;
    }
}
